# from cryptography.hazmat.backends import default_backend
# from cryptography.hazmat.primitives import hashes
# from cryptography.hazmat.primitives.asymmetric import padding
# from cryptography.hazmat.primitives.asymmetric import rsa
# from common.sys_settings import sysSetting
import json


# 公钥加密，私钥解密，私钥签名，公钥验证
# 在建立连接后，由客户端发送公钥给服务器

# def generate_key_pair() -> tuple:
#     """
#     获取非对称密钥
#     Returns:
#
#     """
#     private_key = rsa.generate_private_key(
#         public_exponent=65537,
#         key_size=2048,
#         backend=default_backend()
#     )
#     public_key = private_key.public_key()
#     return public_key, private_key
#
#
# def sign_message(message, private_key) -> bytes:
#     """
#     生成数字签名
#     Args:
#         message:
#         private_key:
#
#     Returns:
#
#     """
#     signature = private_key.sign(
#         message,
#         padding.PSS(
#             mgf=padding.MGF1(hashes.SHA256()),
#             salt_length=padding.PSS.MAX_LENGTH
#         ),
#         hashes.SHA256()
#     )
#     return signature
#
#
# def verify_signature(message, signature, public_key) -> bool:
#     """
#     验证签名
#     Args:
#         message:
#         signature:
#         public_key:
#
#     Returns:
#
#     """
#     try:
#         public_key.verify(
#             signature,
#             message,
#             padding.PSS(
#                 mgf=padding.MGF1(hashes.SHA256()),
#                 salt_length=padding.PSS.MAX_LENGTH
#             ),
#             hashes.SHA256()
#         )
#         return True
#     except Exception as e:
#         return False
#
#
# def generate_message_package(message: str, private_key) -> str:
#     """
#     打包数据
#         header
#     Args:
#         message:
#         private_key:
#
#     Returns:
#
#     """
#     signature = sign_message(message.encode('utf-8'), private_key)
#     length = len(message)
#     data = {
#         'header': {
#             'type': sysSetting.message,
#             'sign': signature,
#             'length': length
#         },
#         'data': message
#     }
#     data = json.dumps(data)
#     return data
#
#
# def encrypt(message: str, public_key: rsa.RSAPublicKey) -> bytes:
#     data = message.encode('utf-8')
#     data = public_key.encrypt(
#         data,
#         padding.OAEP(
#             mgf=padding.MGF1(algorithm=hashes.SHA256()),
#             algorithm=hashes.SHA256(),
#             label=None
#         )
#     )
#     data = data + sysSetting.separator  # 添加包的结束标志
#     return data
#
#
# def decrypt(message: bytes, private_key: rsa.RSAPrivateKey) -> bytes:
#     data = private_key.decrypt(
#         message,
#         padding.OAEP(
#             mgf=padding.MGF1(algorithm=hashes.SHA256()),
#             algorithm=hashes.SHA256(),
#             label=None
#         )
#     )
#     return data


class net:
    """
    格式化数据
    message = {
        "type":type,
        "data":data
    }
    """
    @classmethod
    def encode(cls, _data, _type_of_message, encoding, separator) -> bytes:
        """
        返回值:message:bytes
        """
        _message = {
            'type': _type_of_message,
            'data': _data
        }
        _message = json.dumps(_message).encode(encoding) + separator
        return _message

    @classmethod
    def decode(cls, _message: bytes, encoding, separator) -> tuple:
        """
        返回值:type,data
        """
        _message = json.loads(_message.strip(separator).decode(encoding))
        _type = _message['type']
        _data = _message['data']
        return _type, _data
